/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wehealth.three.lib_connect.base;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.wehealth.three.lib_connect.R;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
public class EcgBaseActivity extends AppCompatActivity {

    private InputMethodManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    /**
     * 隐藏软键盘
     */
    protected void hideKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected void isWait(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.friend_notify));
        builder.setMessage(message);
        builder.setPositiveButton(getResources().getString(R.string.btn_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        builder.show();
    }

    /**
     * 对话框 点击确定结束
     **/
    protected void noticeFinishDialog(String message) {
        if (isFinishing()) {
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.friend_notify));
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton(getResources().getString(R.string.btn_ok), (dialog, which) -> {
            dialog.dismiss();
            finish();
        });
        builder.show();
    }

    /**
     * 对话框 点击确定结束
     **/
    protected void noticeDialog(String message, Function0<Unit> action) {
        if (isFinishing()) {
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.friend_notify));
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton(getResources().getString(R.string.btn_retry), (dialog, which) -> {
            dialog.dismiss();
            action.invoke();

        });
        builder.setNegativeButton(getResources().getString(R.string.btn_cancel), (dialog, which) -> {
            dialog.dismiss();
            finish();

        });
        AlertDialog dialog = builder.show();
        Button ok = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        ok.setAllCaps(false);

        Button cancel = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        cancel.setAllCaps(false);

    }
}
